package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import com.tensquare.util.IdWorker;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 * 业务层代码
 */
@Service
public class LabelService {

    @Autowired
    private LabelDao labelDao;

    @Autowired
    private IdWorker idWorker;
    
    /**
     * 条件分页查询
     * @param page
     * @param size
     * @param label
     * @return
     */
	public Page<Label> search(int page, int size, Label label) {
		//拼接查询条件
		Specification<Label> spec = createSpecification(label);
		//条件分页查询
		return labelDao.findAll(spec, PageRequest.of(page-1, size));
	}
	
	/**
     * 条件查询
     * @param label
     * @return
     */
	public List<Label> search(Label label) {
		//拼接查询条件
		Specification<Label> spec = createSpecification(label);
		//dao中的动态查询
		return labelDao.findAll(spec);
	}

    //封装查询条件
    private Specification<Label> createSpecification(Label label) {
		// TODO 拼接查询条件
		return new Specification<Label>() {
			@Override
			public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//装条件
				List<Predicate> preList = new ArrayList<Predicate>();
				//=================================================================
				//判断标签名称
				if(StringUtils.isNotBlank(label.getLabelname())) {
					//模糊查询
					Predicate like = cb.like(root.get("labelname"), label.getLabelname()+"%");
					//添加到集合中
					preList.add(like);
				}
				
				//判断标签名称
				if(StringUtils.isNotBlank(label.getState())) {
					//精确查询
					Predicate state = cb.equal(root.get("state"), label.getState());
					//添加到集合中
					preList.add(state);
				}
				//=================================================================
				//查询条件
				Predicate[] restrictions = new Predicate[preList.size()];
				//把集合的条件转移到数组中
				preList.toArray(restrictions);
				//完成动态查询
				return cb.and(restrictions);
			}
		};
	}


    //查询所有
    public List<Label> findAll(){
        return labelDao.findAll();
    }
    //根据id查询一个
    public Label findById(String id){
        int i = new Random().nextInt(1000);
        if(i<500){
            throw  new RuntimeException("抛一个异常！！！！"+i);
        }
        return labelDao.findById(id).get();
    }
    //新增
    @Transactional
    public void add(Label label){
        label.setId(idWorker.nextId()+"");//加入主键
        labelDao.save(label);
    }
    //修改
    @Transactional
    public void update(Label label){
        labelDao.save(label);
    }
    //删除
    @Transactional
    public void deleteById(String id){
        labelDao.deleteById(id);
    }


}
